<?php
/*
Plugin Name: Simplr Embed Video
Plugin URI: http://www.i.ph/
Description: Embed Videos to WP as simple and as lite as possible
Version: 0.01
Date: November 28, 2007
Author: Brian Feliciano, i.PH Dev Team
Author URI: http://redbaks.i.ph/
--------------------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
--------------------------------------------------------------------------------
0.01 (28-Nov-2007) 
  - created it 
  - supports YouTube, GoogleVideo and MetaCafe
*/ 
define(SimplrEV_VERSION,'0.01');

$SimplrEV = array();
$SimplrEV['SUPPORTED'] = array (
  /** YouTube Support ***/
  'www.youtube.com' => array (
      'name'  =>'YouTube.com',
      'link'  =>'http://www.youtube.com',
      'embed' => create_function ('$p_url',
         '$qry   = _swpev_parseQuery($p_url["query"]);'.
         '$movie = $p_url["scheme"]."://".$p_url["host"] . "/v/" .$qry["v"];'.
         'return "<object data=\"$movie\" type=\"application/x-shockwave-flash\" ".
                          " width=\"100%\" height=\"100%\">".
                      "<param name=\"movie\" value=\"$movie\"></param>".
                      "<param name=\"wmode\" value=\"transparent\"></param>".
                      "<embed type=\"application/x-shockwave-flash\" ".
                              "src=\"$movie\" wmode=\"transparent\"></embed></object>";')),

  /** Google Video Support ***/
  'video.google.com' => array (
      'name'  =>'Google Video',
      'link'  =>'http://video.google.com',
      'embed' => create_function ('$p_url',
        '$qry = _swpev_parseQuery($p_url["query"]);'.
        '$movie = $p_url["scheme"]."://".$p_url["host"]."/googleplayer.swf?docId=".$qry["docid"]."&hl=en";'.
        'return "<embed src=\"$movie\" style=\"width:100%; height:100%;\" ".
                       "wmode=\"transparent\" type=\"application/x-shockwave-flash\"></embed>";')),

  /** Meta Cafe Support **/
  'www.metacafe.com' => array (
      'name'  =>'Meta Cafe',
      'link'  =>'http://www.metacafe.com',
      'embed' => create_function ('$p_url',
        '$path = explode("/",$p_url["path"]);'.
        '$path = _swpev_trimArray($path);'.
        '$movie = $p_url["scheme"]."://".$p_url["host"]."/fplayer/".$path[1]."/".$path[2].".swf";'.
        'return "<embed src=\"$movie\" width=\"100%\" height=\"100%\" wmode=\"transparent\" ".
                      " pluginspage=\"http://www.macromedia.com/go/getflashplayer\" ".
                      " type=\"application/x-shockwave-flash\"></embed>";'))
);
//clone for youtube.com
$SimplrEV['SUPPORTED']['youtube.com'] = & $SimplrEV['SUPPORTED']['www.youtube.com'];
//clone for metacafe
$SimplrEV['SUPPORTED']['metacafe.com'] = & $SimplrEV['SUPPORTED']['www.metacafe.com'];


//Miscellaneous Functions
function _swpev_trimArray($ary) {
  $newary = array();
  foreach ($ary as $i) { if ($i) $newary[]=$i; }
  return $newary;
}

if ( !function_exists('htmlspecialchars_decode') )
{
   function htmlspecialchars_decode($text)
   {
       return strtr($text, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
   }
}

function _swpev_parseParams($pstr) {
  $pary = array();
  $params = explode(' ',$pstr);
  foreach ($params as $param) {
    if (trim($param)!=='') {
      list ($k,$v) = explode('=',$param);
      $pary[$k]=$v;
    }
  }
  return $pary;
}

function _swpev_parseQuery($qry) {
  $ary = array();
  $aqry = explode('&',$qry);
  foreach($aqry as $val) { list ($k, $v) = explode('=',$val); $ary[$k]=$v; }
  return $ary;
}

function SimplrEV_embed($url='') {
  global $SimplrEV;

  $content = $url;
  $v_url = parse_url($url);
  /* check here */
  if (!is_array($v_url) || !isset($v_url['host'])) return "Unable to embed this url. [$url]";

  if (isset($SimplrEV['SUPPORTED'][$v_url['host']])) {
    $content = $SimplrEV['SUPPORTED'][$v_url['host']]['embed']($v_url);
  } else {
    $content = '-not yet supported- <br/> ' . $url;
  }

  return $content;
}

function SimplrEV($text) {
  $video_pattern = '/([\[|<]video\s*(.*?)[\]|>](.*?)[\[|<]\/video[\]|>])/i';

  if (preg_match_all ($video_pattern, $text, $matches)) {
    $embed_css='width:350px !important;height:288px !important;z-index:0  !important;';
    for ($i=0;$i<count($matches[0]);$i++) {
      $p= _swpev_parseParams($matches[2][$i]);
      $p['align'] = (preg_match('/align-(center|right|left)/i', $p['align'], $dm)) ? $dm[0] : 'align-left';

      switch (strtolower($p['align'])) {
        case 'align-center':
          $embed_css.='margin: 0 auto;';
          break;
        case 'align-right':
          $embed_css.='margin:0 0 0 auto;';
          break;
        case 'align-left':
        default:
          $embed_css.='margin:0;';
          break;
      }

      $html = '<div style="'.$embed_css.'">'. SimplrEV_embed($matches[3][$i]) . '</div>';
      $text=str_replace( $matches[0][$i], $html, $text );
    }
  }
  return $text;
}

add_filter('the_content', 'SimplrEV');
?>
